<template>
  <div class="login-container">
    <div class="animated-bg">
      <div class="bg-circle circle-1"></div>
      <div class="bg-circle circle-2"></div>
      <div class="bg-circle circle-3"></div>
      <div class="bg-pattern"></div>
    </div>
    
    <div class="login-wrapper">
      <div class="login-card">
        <div class="login-header">
          <div class="login-logo">
            <div class="logo-image"></div>
            <h1>博客管理系统</h1>
          </div>
          <p class="login-subtitle">请登录您的管理员账户</p>
        </div>
        
        <div class="login-form">
          <div class="alert alert-danger" v-if="error">
            <i class="fas fa-exclamation-circle"></i>
            <span>{{ error }}</span>
          </div>
          
          <form @submit.prevent="handleLogin">
            <div class="form-group">
              <label for="username">
                <i class="fas fa-user"></i>
                <span>用户名</span>
              </label>
              <div class="input-wrapper">
                <input 
                  type="text" 
                  id="username" 
                  v-model="credentials.username" 
                  required
                  placeholder="请输入用户名"
                  autocomplete="username"
                  :disabled="isSubmitting"
                />
                <span class="input-focus-border"></span>
              </div>
            </div>
            
            <div class="form-group">
              <label for="password">
                <i class="fas fa-lock"></i>
                <span>密码</span>
              </label>
              <div class="input-wrapper password-input">
                <input 
                  :type="showPassword ? 'text' : 'password'" 
                  id="password" 
                  v-model="credentials.password" 
                  required
                  placeholder="请输入密码"
                  autocomplete="current-password"
                  :disabled="isSubmitting"
                />
                <span class="input-focus-border"></span>
                <button 
                  type="button" 
                  class="toggle-password" 
                  @click="showPassword = !showPassword"
                >
                  <i :class="showPassword ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
                </button>
              </div>
            </div>
            
            <div class="form-group form-options">
              <div class="remember-me">
                <input type="checkbox" id="remember" v-model="credentials.remember" />
                <label for="remember">记住我</label>
              </div>
              <a href="#" class="forgot-password">忘记密码?</a>
            </div>
            
            <div class="form-group">
              <button type="submit" class="login-button" :disabled="isSubmitting">
                <span v-if="!isSubmitting">登录</span>
                <span v-else class="loading-spinner">
                  <i class="fas fa-circle-notch fa-spin"></i>
                </span>
              </button>
            </div>
          </form>
        </div>
        
        <div class="login-footer">
          <p>首次登录? 请联系系统管理员获取账户</p>
          <a href="/" class="back-to-site">
            <i class="fas fa-arrow-left"></i> 返回网站首页
          </a>
        </div>
      </div>
      
      <div class="login-design-element el-1"></div>
      <div class="login-design-element el-2"></div>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { authService } from '../../services';

export default {
  setup() {
    const router = useRouter();
    const route = useRoute();
    
    const credentials = reactive({
      username: '',
      password: '',
      remember: false
    });
    
    const error = ref('');
    const isSubmitting = ref(false);
    const showPassword = ref(false);
    
    const handleLogin = async () => {
      try {
        error.value = '';
        isSubmitting.value = true;
        
        const result = await authService.login(credentials.username, credentials.password, credentials.remember);
        
        if (result.token) {
          // 登录成功，重定向到管理后台或指定的重定向URL
          const redirectPath = route.query.redirect || '/admin/dashboard';
          router.push(redirectPath);
        }
      } catch (err) {
        console.error('登录失败:', err);
        error.value = err.response?.data?.message || '登录失败，请检查您的用户名和密码。';
      } finally {
        isSubmitting.value = false;
      }
    };
    
    return {
      credentials,
      error,
      isSubmitting,
      showPassword,
      handleLogin
    };
  }
}
</script>

<style scoped>
:root {
  --primary-color: #4361ee;
  --secondary-color: #3a0ca3;
  --success-color: #2a9d8f;
  --warning-color: #ff9914;
  --danger-color: #e63946;
  --light-color: #f8f9fa;
  --dark-text: #2d3748;
  --light-text: #f8f9fa;
}

/* 全屏背景容器 */
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fb;
  position: relative;
  overflow: hidden;
}

/* 动画背景 */
.animated-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  overflow: hidden;
}

.bg-circle {
  position: absolute;
  border-radius: 50%;
  filter: blur(50px);
  opacity: 0.7;
  animation: float 15s infinite ease-in-out;
}

.circle-1 {
  width: 600px;
  height: 600px;
  background: linear-gradient(45deg, #4361ee, #4cc9f0);
  top: -200px;
  right: -200px;
  animation-delay: 0s;
}

.circle-2 {
  width: 500px;
  height: 500px;
  background: linear-gradient(45deg, #3a0ca3, #7209b7);
  bottom: -150px;
  left: -100px;
  animation-delay: 5s;
}

.circle-3 {
  width: 300px;
  height: 300px;
  background: linear-gradient(45deg, #f72585, #ff9914);
  top: 30%;
  left: 10%;
  animation-delay: 2.5s;
}

.bg-pattern {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: 
    linear-gradient(rgba(255, 255, 255, 0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.05) 1px, transparent 1px);
  background-size: 50px 50px;
  z-index: 1;
}

@keyframes float {
  0% {
    transform: translate(0, 0) rotate(0deg);
  }
  50% {
    transform: translate(30px, 20px) rotate(5deg);
  }
  100% {
    transform: translate(0, 0) rotate(0deg);
  }
}

/* 登录卡片包装器 */
.login-wrapper {
  position: relative;
  z-index: 2;
  max-width: 95%;
  animation: fadeIn 1s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 设计元素 */
.login-design-element {
  position: absolute;
  border-radius: 50%;
  z-index: -1;
}

.el-1 {
  width: 120px;
  height: 120px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
  top: -40px;
  right: -20px;
  animation: pulse 5s infinite alternate;
}

.el-2 {
  width: 80px;
  height: 80px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
  bottom: -30px;
  left: -20px;
  animation: pulse 5s infinite alternate-reverse;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  100% {
    transform: scale(1.2);
  }
}

/* 登录卡片 */
.login-card {
  width: 450px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  position: relative;
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 5px;
  background: linear-gradient(90deg, #4361ee, #3a0ca3);
}

/* 头部样式 */
.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
}

.logo-image {
  width: 80px;
  height: 80px;
  border-radius: 15px;
  background: linear-gradient(135deg, #4361ee, #3a0ca3);
  margin-bottom: 15px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-size: 28px;
  font-weight: bold;
}

.logo-image::after {
  content: 'B';
  font-size: 40px;
  font-weight: 700;
  color: white;
}

.login-logo h1 {
  font-size: 24px;
  font-weight: 700;
  color: var(--dark-text);
  margin: 0;
}

.login-subtitle {
  font-size: 14px;
  color: #666;
  margin-top: 5px;
}

/* 表单样式 */
.login-form {
  margin-bottom: 30px;
}

.alert {
  padding: 15px;
  margin-bottom: 20px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.alert-danger {
  background-color: rgba(230, 57, 70, 0.1);
  color: var(--danger-color);
  border-left: 4px solid var(--danger-color);
}

.form-group {
  margin-bottom: 25px;
}

label {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  font-weight: 600;
  color: var(--dark-text);
  margin-bottom: 10px;
}

label i {
  color: var(--primary-color);
}

.input-wrapper {
  position: relative;
}

input[type="text"], 
input[type="password"] {
  width: 100%;
  padding: 12px 15px;
  font-size: 14px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  transition: all 0.3s ease;
  background-color: rgba(255, 255, 255, 0.7);
}

input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.15);
}

.input-focus-border {
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background: var(--primary-color);
  transition: all 0.3s ease;
  transform: translateX(-50%);
}

input:focus + .input-focus-border {
  width: 100%;
}

.password-input {
  display: flex;
  align-items: center;
}

.toggle-password {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
  padding: 0;
  font-size: 14px;
  transition: all 0.3s ease;
}

.toggle-password:hover {
  color: var(--primary-color);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
}

.remember-me input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--primary-color);
}

.remember-me label {
  margin: 0;
  font-size: 13px;
  font-weight: normal;
  color: #666;
}

.forgot-password {
  font-size: 13px;
  color: var(--primary-color);
  text-decoration: none;
  transition: all 0.3s ease;
}

.forgot-password:hover {
  text-decoration: underline;
}

.login-button {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #4361ee, #3a0ca3);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(67, 97, 238, 0.3);
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(67, 97, 238, 0.4);
}

.login-button:disabled {
  background: #aaa;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.loading-spinner {
  animation: spin 1s infinite linear;
  display: inline-block;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 页脚样式 */
.login-footer {
  text-align: center;
}

.login-footer p {
  font-size: 13px;
  color: #666;
  margin-bottom: 15px;
}

.back-to-site {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--primary-color);
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
}

.back-to-site:hover {
  color: var(--secondary-color);
}

/* 响应式适配 */
@media (max-width: 576px) {
  .login-card {
    width: 100%;
    padding: 30px 20px;
  }
  
  .logo-image {
    width: 60px;
    height: 60px;
  }
  
  .login-logo h1 {
    font-size: 20px;
  }
  
  .form-options {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }
  
  .forgot-password {
    align-self: flex-end;
  }
}
</style> 